//
//  WPMaleCaseCVC.m
//  wobingwoyi
//
//  Created by WGP on 16/9/8.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPMaleCaseCVC.h"
#import "WPBodyTableViewCell.h"
#import "WPManDiseaseModel.h"

@interface WPMaleCaseCVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIImageView *bgImageView;
@property(nonatomic,strong)UIButton *headerBtn;
@property(nonatomic,strong)UIButton *chestBtn;
@property(nonatomic,strong)UIButton *leftHandBtn;
@property(nonatomic,strong)UIButton *rightHandBtn;
@property(nonatomic,strong)UIButton *fubuBtn;
@property(nonatomic,strong)UIButton *danbuBtn;
@property(nonatomic,strong)UIButton *leftLegBtn;
@property(nonatomic,strong)UIButton *rightLegBtn;
@property(nonatomic,strong)UIButton *currentBtn;
@property(nonatomic,strong)UIButton *coverBtn;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *partArr;

@end
@implementation WPMaleCaseCVC

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self bgImageView];
    [self chestBtn];
    [self headerBtn];
    [self fubuBtn];
    [self leftHandBtn];
    [self rightHandBtn];
    [self danbuBtn];
    [self leftLegBtn];
    [self rightLegBtn];
}

-(UIImageView *)bgImageView
{
    if (_bgImageView == nil)
    {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgCase"]];
        [self.contentView addSubview:_bgImageView];
        
        [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return _bgImageView;
}

-(UIButton *)headerBtn
{
    if (_headerBtn == nil)
    {
        _headerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_headerBtn setBackgroundImage:[UIImage imageNamed:@"header"] forState:UIControlStateNormal];
        [_headerBtn setBackgroundImage:[UIImage imageNamed:@"header_highlight"] forState:UIControlStateSelected];
        [_headerBtn addTarget:self action:@selector(bodyBtnDidClick:) forControlEvents:UIControlEventTouchDown];
        
        [self.contentView addSubview:_headerBtn];
        
        [_headerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(18*kMoreDetailCaseScale);
            make.width.mas_equalTo(57.5*kMoreDetailCaseScale);
            make.height.mas_equalTo(76*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(157*kMoreDetailCaseScale);
        }];
    }
    return _headerBtn;
}

-(UIButton *)chestBtn
{
    if (_chestBtn == nil)
    {
        _chestBtn = [self buttonWithImageName:@"chest"];
        
        [_chestBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(83*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(120*kMoreDetailCaseScale);
            make.width.mas_equalTo(136*kMoreDetailCaseScale);
            make.height.mas_equalTo(96*kMoreDetailCaseScale);
        }];
    }
    return _chestBtn;
}

-(UIButton *)fubuBtn
{
    if (_fubuBtn == nil)
    {
        _fubuBtn = [self buttonWithImageName:@"fubu"];
        
        [_fubuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(145*kMoreDetailCaseScale);
            make.width.mas_equalTo(83*kMoreDetailCaseScale);
            make.height.mas_equalTo(76*kMoreDetailCaseScale);
            make.top.equalTo(self.contentView).offset(174*kMoreDetailCaseScale);
        }];
    }
    return _fubuBtn;
}

-(UIButton *)leftHandBtn
{
    if (_leftHandBtn == nil)
    {
        _leftHandBtn = [self buttonWithImageName:@"leftHand"];
        
        [_leftHandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(86*kMoreDetailCaseScale);
            make.height.mas_equalTo(172*kMoreDetailCaseScale);
            make.top.equalTo(self.contentView).offset(136*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(62*kMoreDetailCaseScale);
        }];
    }
    return _leftHandBtn;
}

-(UIButton *)rightHandBtn
{
    if (_rightHandBtn == nil)
    {
        _rightHandBtn = [self buttonWithImageName:@"rightHand"];
        
        [_rightHandBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(86*kMoreDetailCaseScale);
            make.height.mas_equalTo(172*kMoreDetailCaseScale);
            make.top.equalTo(self.contentView).offset(135*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(226*kMoreDetailCaseScale);
        }];
    }
    return _rightHandBtn;
}

-(UIButton *)danbuBtn
{
    if (_danbuBtn == nil)
    {
        _danbuBtn = [self buttonWithImageName:@"reproductive"];
        
        [_danbuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(82*kMoreDetailCaseScale);
            make.height.mas_equalTo(52*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(145*kMoreDetailCaseScale);
            make.top.equalTo(self.contentView).offset(240*kMoreDetailCaseScale);
        }];
    }
    return _danbuBtn;
}

-(UIButton *)leftLegBtn
{
    if (_leftLegBtn == nil)
    {
        _leftLegBtn = [self buttonWithImageName:@"leftLeg"];
        
        [_leftLegBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(52*kMoreDetailCaseScale);
            make.height.mas_equalTo(277*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(130*kMoreDetailCaseScale);
            make.top.equalTo(self.contentView).offset(243*kMoreDetailCaseScale);
        }];
    }
    return _leftLegBtn;
}

-(UIButton *)rightLegBtn
{
    if (_rightLegBtn == nil)
    {
        _rightLegBtn = [self buttonWithImageName:@"rightLeg"];
        
        [_rightLegBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(52*kMoreDetailCaseScale);
            make.height.mas_equalTo(277*kMoreDetailCaseScale);
            make.left.equalTo(self.contentView).offset(190*kMoreDetailCaseScale);
            make.top.equalTo(self.contentView).offset(243*kMoreDetailCaseScale);
        }];
    }
    return _rightLegBtn;
}

-(UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc] init];
        _tableView.layer.cornerRadius = 8;
        _tableView.layer.masksToBounds = YES;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.bounces = NO;
        _tableView.rowHeight = 45;
        [_tableView registerClass:[WPBodyTableViewCell class] forCellReuseIdentifier:@"WPBodyTableViewCell"];
    }
    return _tableView;
}

#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.partArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPBodyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPBodyTableViewCell"];
    cell.textLabel.text = self.partArr[indexPath.item];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.diseaseDidClickBlock)
    {
        self.diseaseDidClickBlock(self.partArr[indexPath.item]);
        [self coverBtnDidClick:self.coverBtn];
    }
}

-(UIButton *)buttonWithImageName:(NSString *)name
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:name] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlight",name]] forState:UIControlStateSelected];
    [self.contentView addSubview:btn];
    [btn addTarget:self action:@selector(bodyBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    return btn;
}

-(void)bodyBtnDidClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    self.currentBtn = btn;
    
    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.coverBtn = coverBtn;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    coverBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.5);
    [keyWindow addSubview:coverBtn];
    coverBtn.frame = [UIScreen mainScreen].bounds;
    [coverBtn addTarget:self action:@selector(coverBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    if (btn == self.chestBtn)
    {
        self.tableView.frame = CGRectMake(209*kMoreDetailCaseScale, 125*kMoreDetailCaseScale+108, kTableViewWidth, kTableViewHeight);
        [coverBtn addSubview:self.tableView];
    }
    else if (btn == self.headerBtn)
    {
        self.tableView.frame = CGRectMake(210*kMoreDetailCaseScale, 35*kMoreDetailCaseScale+108, kTableViewWidth, kTableViewHeight);
        [coverBtn addSubview:self.tableView];
    }
    else if (btn == self.fubuBtn)
    {
        self.tableView.frame = CGRectMake(15*kMoreDetailCaseScale, 187*kMoreDetailCaseScale+108, kTableViewWidth, kTableViewHeight);
        [coverBtn addSubview:self.tableView];
    }
    else if (btn == self.leftHandBtn || btn == self.rightHandBtn)
    {
        self.leftHandBtn.selected = YES;
        self.rightHandBtn.selected = YES;
        
        self.tableView.frame = CGRectMake(15*kMoreDetailCaseScale, 67*kMoreDetailCaseScale+108, kTableViewWidth, kTableViewHeight);
        [coverBtn addSubview:self.tableView];
    }
    else if(btn == self.danbuBtn)
    {
        self.tableView.frame = CGRectMake(210*kMoreDetailCaseScale, 257*kMoreDetailCaseScale+108, kTableViewWidth, kTableViewHeight);
        [coverBtn addSubview:self.tableView];
    }
    else if (btn == self.leftLegBtn || btn == self.rightLegBtn)
    {
        self.leftLegBtn.selected = YES;
        self.rightLegBtn.selected = YES;
        
        self.tableView.frame = CGRectMake(15*kMoreDetailCaseScale, 282.5*kMoreDetailCaseScale+108, kTableViewWidth, kTableViewHeight);
        [coverBtn addSubview:self.tableView];
    }
    [self.tableView reloadData];
}

-(void)coverBtnDidClick:(UIButton *)btn
{
    self.currentBtn.selected = NO;
    if (self.currentBtn == self.leftHandBtn || self.currentBtn == self.rightHandBtn)
    {
        self.leftHandBtn.selected = NO;
        self.rightHandBtn.selected = NO;
    }
    
    if (self.currentBtn == self.leftLegBtn || self.currentBtn == self.rightLegBtn)
    {
        self.leftLegBtn.selected = NO;
        self.rightLegBtn.selected = NO;
    }
    [btn removeFromSuperview];
    btn = nil;
}

-(void)setCurrentBtn:(UIButton *)currentBtn
{
    _currentBtn = currentBtn;
    
    if (currentBtn == self.headerBtn)
    {
        self.partArr = self.model.head;
    }
    else if (currentBtn == self.chestBtn)
    {
        self.partArr = self.model.chest;
    }
    else if (currentBtn == self.fubuBtn)
    {
        self.partArr = self.model.abdomen;
    }
    else if (currentBtn == self.danbuBtn)
    {
        self.partArr = self.model.genital;
    }
    else if(currentBtn == self.leftHandBtn || currentBtn == self.rightHandBtn)
    {
        self.partArr = self.model.hand;
    }
    else
    {
        self.partArr = self.model.foot;
    }
}

@end




